#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/printk.h>
// 定义模块信息
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Luo");
MODULE_DESCRIPTION("A simple module for proccess");
MODULE_VERSION("1.0");

// 初始化函数
static int __init process_init(void) {
    printk(KERN_INFO "ProcessInfo: start scan\n");
    printk(KERN_INFO "ProcessInfo: PID\tName\t\tState\n");
    printk(KERN_INFO "ProcessInfo: ---\t----\t\t-----\n");
    struct task_struct *task;
    rcu_read_lock();
    for_each_process(task) {
        const char *state;
        if (task->__state & TASK_RUNNING) {
            state = "RUNNING";
        } 
        else if (task->__state & TASK_INTERRUPTIBLE) {
            state = "INTERRUPTIBLE";
        }
        else if (task->__state & TASK_UNINTERRUPTIBLE) {
            state = "UNINTERRUPTIBLE";
        }
        else {
            state = "OTHER";
        }
        printk(KERN_INFO "ProcessInfo: %d\t%s\t\t%s\n", task->pid, task->comm, state);
    }
    rcu_read_unlock();
    return 0;
}
// 退出函数
static void __exit process_exit(void) {
    pr_info("ProcessInfo: end scan\n");
}

module_init(process_init);
module_exit(process_exit);